<!DOCTYPE html>




<html class="theme-next gemini" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="Android测试,自动化测试,Android UIAutomator," />





  <link rel="alternate" href="/atom.xml" title="流水不腐小夏" type="application/atom+xml" />






<meta name="description" content="Android UIAutomator浅谈 [TOC] 简介Uiautomator是谷歌推出的，用于UI自动化测试的工具，也就是普通的手工测试，点击每个控件元素看看输出的结果是否符合预期。比如登陆界面分别输入正确和错误的用户名密码然后点击登陆按钮看看是否能否登陆以及是否有错误提示等。 注意：UI Automator测试框架是基于instrumentation的API，运行在Android Juni">
<meta name="keywords" content="Android测试,自动化测试,Android UIAutomator">
<meta property="og:type" content="article">
<meta property="og:title" content="Android UIAutomator">
<meta property="og:url" content="http://mycommons.cn/2016/07/25/Android-UIAutomator/index.html">
<meta property="og:site_name" content="流水不腐小夏">
<meta property="og:description" content="Android UIAutomator浅谈 [TOC] 简介Uiautomator是谷歌推出的，用于UI自动化测试的工具，也就是普通的手工测试，点击每个控件元素看看输出的结果是否符合预期。比如登陆界面分别输入正确和错误的用户名密码然后点击登陆按钮看看是否能否登陆以及是否有错误提示等。 注意：UI Automator测试框架是基于instrumentation的API，运行在Android Juni">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2016-07-25T13:26:12.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android UIAutomator">
<meta name="twitter:description" content="Android UIAutomator浅谈 [TOC] 简介Uiautomator是谷歌推出的，用于UI自动化测试的工具，也就是普通的手工测试，点击每个控件元素看看输出的结果是否符合预期。比如登陆界面分别输入正确和错误的用户名密码然后点击登陆按钮看看是否能否登陆以及是否有错误提示等。 注意：UI Automator测试框架是基于instrumentation的API，运行在Android Juni">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'GRLHBBCD9E',
      apiKey: '759ab3f5c09b132b3ed4842a44c53c45',
      indexName: 'mycommons',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://mycommons.cn/2016/07/25/Android-UIAutomator/"/>





  <title>Android UIAutomator | 流水不腐小夏</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?55458ffca1f60dd283f11b9a7f82b262";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">流水不腐小夏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">http://mycommons.cn</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://mycommons.cn/2016/07/25/Android-UIAutomator/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="流水不腐小夏">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="流水不腐小夏">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Android UIAutomator</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2016-07-25T21:25:22+08:00">
                2016-07-25
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android测试/" itemprop="url" rel="index">
                    <span itemprop="name">Android测试</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2016/07/25/Android-UIAutomator/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2016/07/25/Android-UIAutomator/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Android-UIAutomator浅谈"><a href="#Android-UIAutomator浅谈" class="headerlink" title="Android UIAutomator浅谈"></a>Android UIAutomator浅谈</h1><hr>
<p>[TOC]</p>
<h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>Uiautomator是谷歌推出的，用于UI自动化测试的工具，也就是普通的手工测试，点击每个控件元素看看输出的结果是否符合预期。比如登陆界面分别输入正确和错误的用户名密码然后点击登陆按钮看看是否能否登陆以及是否有错误提示等。</p>
<p>注意：UI Automator测试框架是基于instrumentation的API，运行在Android JunitRunner 之上，同时UI Automator Test只运行在Android 4.3(API level 18)以上版本。</p>
<h1 id="准备"><a href="#准备" class="headerlink" title="准备"></a>准备</h1><p>集成UI Automator，首先需要保证App项目已经依赖了Gradle Testing。然后在gradle中添加如下依赖即可。</p>
<figure class="highlight groovy"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">dependencies &#123;</div><div class="line">    androidTestCompile <span class="string">'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'</span></div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="UI元素查找"><a href="#UI元素查找" class="headerlink" title="UI元素查找"></a>UI元素查找</h1><p>在Android开发中，可以种findViewById来找到相应的控件，但是UI Automator是用来测试界面的，如果也是用相同的方式，那么则会依赖很大。UI Automator使用一种类似的方式来查找元素。UI Automator是根据元素的text、hint、contentDescription等属性进行查找的，为了使UI Automator能够获取到以上的属性值，请测试的UI界面是可以访问的，同时UI Automator也是可以访问这些控件元素。</p>
<p>可以使用uiautomatorviewer，来获取界面元素的层次关系以及各个元素的属性，然后UI Automator就可以根据这些属性信息查找到对应的控件元素。</p>
<p>获取步骤：</p>
<ul>
<li>启动设备上的App</li>
<li>把设备连接到开发机器上</li>
<li>打开Android sdk目录，<code>&lt;android-sdk&gt;/tools/</code>。</li>
<li>启动<code>uiautomatorviewer</code><br>然后可以操作<code>uiautomatorviewer</code></li>
<li>点击Device ScreenShot，稍等一会，获取到了屏幕的快照，界面右侧是当前界面的布局结构以及属性信息。</li>
</ul>
<p><strong>确保UI可以访问</strong><br>上面介绍过，UI Automator是根据元素的text、hint、contentDescription等属性进行查找的，所以需要尽可能给布局中的元素添加上面的属性。有时候程序员需要自定义一些控件，那么请实现<code>AccessibilityNodeProvider</code>，以确保可以正常使用。</p>
<h2 id="访问UI控件"><a href="#访问UI控件" class="headerlink" title="访问UI控件"></a>访问UI控件</h2><p>UI Automator 提供 <code>UiDevice</code> 类，这个类提供一些方法，可以获取设备的一些状态和属性，同时可以执行一系列动作来操作设备。下面是一个示例，演示怎么样货到 UiDevice 对象，以及按下Home键。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> org.junit.Before;</div><div class="line"><span class="keyword">import</span> android.support.test.runner.AndroidJUnit4;</div><div class="line"><span class="keyword">import</span> android.support.test.uiautomator.UiDevice;</div><div class="line"><span class="keyword">import</span> android.support.test.uiautomator.By;</div><div class="line"><span class="keyword">import</span> android.support.test.uiautomator.Until;</div><div class="line">...</div><div class="line"></div><div class="line"><span class="meta">@RunWith</span>(AndroidJUnit4.class)</div><div class="line"><span class="meta">@SdkSuppress</span>(minSdkVersion = <span class="number">18</span>)</div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ChangeTextBehaviorTest</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String BASIC_SAMPLE_PACKAGE</div><div class="line">            = <span class="string">"com.example.android.testing.uiautomator.BasicSample"</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> LAUNCH_TIMEOUT = <span class="number">5000</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String STRING_TO_BE_TYPED = <span class="string">"UiAutomator"</span>;</div><div class="line">    <span class="keyword">private</span> UiDevice mDevice;</div><div class="line"></div><div class="line">    <span class="meta">@Before</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">startMainActivityFromHomeScreen</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="comment">// Initialize UiDevice instance</span></div><div class="line">        mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());</div><div class="line"></div><div class="line">        <span class="comment">// Start from the home screen</span></div><div class="line">        mDevice.pressHome();</div><div class="line"></div><div class="line">        <span class="comment">// Wait for launcher</span></div><div class="line">        <span class="keyword">final</span> String launcherPackage = mDevice.getLauncherPackageName();</div><div class="line">        assertThat(launcherPackage, notNullValue());</div><div class="line">        mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(<span class="number">0</span>)),</div><div class="line">                LAUNCH_TIMEOUT);</div><div class="line"></div><div class="line">        <span class="comment">// Launch the app</span></div><div class="line">        Context context = InstrumentationRegistry.getContext();</div><div class="line">        <span class="keyword">final</span> Intent intent = context.getPackageManager()</div><div class="line">                .getLaunchIntentForPackage(BASIC_SAMPLE_PACKAGE);</div><div class="line">        <span class="comment">// Clear out any previous instances</span></div><div class="line">        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);</div><div class="line">        context.startActivity(intent);</div><div class="line"></div><div class="line">        <span class="comment">// Wait for the app to appear</span></div><div class="line">        mDevice.wait(Until.hasObject(By.pkg(BASIC_SAMPLE_PACKAGE).depth(<span class="number">0</span>)),</div><div class="line">                LAUNCH_TIMEOUT);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p> 然后可以使用<code>findObject()</code>方法来获取到界面的UI控件，如下所示，在一个设备上面找出符合规则的UI元素，然后再执行一些列动作。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">UiObject cancelButton = mDevice.findObject(<span class="keyword">new</span> UiSelector()</div><div class="line">        .text(<span class="string">"Cancel"</span>))</div><div class="line">        .className(<span class="string">"android.widget.Button"</span>));</div><div class="line">UiObject okButton = mDevice.findObject(<span class="keyword">new</span> UiSelector()</div><div class="line">        .text(<span class="string">"OK"</span>))</div><div class="line">        .className(<span class="string">"android.widget.Button"</span>));</div><div class="line"></div><div class="line"><span class="comment">// Simulate a user-click on the OK button, if found.</span></div><div class="line"><span class="keyword">if</span>(okButton.exists() &amp;&amp; okButton.isEnabled()) &#123;</div><div class="line">    okButton.click();</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="指定一个选择器"><a href="#指定一个选择器" class="headerlink" title="指定一个选择器"></a>指定一个选择器</h2><p>有时候，需要访问一个特定的UI组件，可以使用<code>UiSelector</code>。这个类提供一些列的方法，帮助找到特定的UI组件。<br>当有多个符合条件UI元素被查找到时候，第一个元素会不是使用。可以传递的一个UiSelector到构造方法，相当于链式访问。如果没有元素被找到，那么会抛出<code>UiAutomatorObjectNotFoundException</code>异常。<br>也可以使用<code>childSelector()</code>来帅选多个UiSelector对象，如下所示，ListView的子元素中有很多是相同的，可以根据这个方法，选择某一个子元素。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">UiObject appItem = <span class="keyword">new</span> UiObject(<span class="keyword">new</span> UiSelector()</div><div class="line">        .className(<span class="string">"android.widget.ListView"</span>)</div><div class="line">        .instance(<span class="number">1</span>)</div><div class="line">        .childSelector(<span class="keyword">new</span> UiSelector()</div><div class="line">        .text(<span class="string">"Apps"</span>)));</div></pre></td></tr></table></figure></p>
<p>上面的查找还是很复杂，我们可以根据ResourceId 来替代文本查找的方式。文本找到的方式很脆弱，而且很容使测试失败，而且当语言环境变化了，文本查找就需要对照多个翻译版本的文本了。</p>
<h1 id="执行动作"><a href="#执行动作" class="headerlink" title="执行动作"></a>执行动作</h1><p>当我们根据文本查找找到对应的文本元素后，我们就可以随心所欲的进行操作了。</p>
<ul>
<li>click() 点击</li>
<li>dragTo() 拖动当前元素</li>
<li>setText() 设置文本</li>
<li>swipeUp() 向上滑动，同理也有向下、向左、向右，swipeDown、swipeLeft、swipeRight。</li>
</ul>
<p>UI Automator也可以提供一个Context，这样可以方便发送一个Intent或者是启动一个Activity。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setUp</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="comment">// Launch a simple calculator app</span></div><div class="line">    Context context = getInstrumentation().getContext();</div><div class="line">    Intent intent = context.getPackageManager()</div><div class="line">            .getLaunchIntentForPackage(CALC_PACKAGE);</div><div class="line">    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);</div><div class="line">            <span class="comment">// Clear out any previous instances</span></div><div class="line">    context.startActivity(intent);</div><div class="line">    mDevice.wait(Until.hasObject(By.pkg(CALC_PACKAGE).depth(<span class="number">0</span>)), TIMEOUT);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="集合操作"><a href="#集合操作" class="headerlink" title="集合操作"></a>集合操作</h2><p>可以使用<code>UiCollection</code>来模拟用户对一组UI控件进行操作，比如有时候界面上有个Listview。为了创建一个 UiCollection 对象，可以指定一个UiSelector搜索这个UI容器上满足这个条件的所有子元素。</p>
<p>下面代码演示如何在一个一组控件中进行操作。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">UiCollection videos = <span class="keyword">new</span> UiCollection(<span class="keyword">new</span> UiSelector()</div><div class="line">        .className(<span class="string">"android.widget.FrameLayout"</span>));</div><div class="line"></div><div class="line"><span class="comment">// Retrieve the number of videos in this collection:</span></div><div class="line"><span class="keyword">int</span> count = videos.getChildCount(<span class="keyword">new</span> UiSelector()</div><div class="line">        .className(<span class="string">"android.widget.LinearLayout"</span>));</div><div class="line"></div><div class="line"><span class="comment">// Find a specific video and simulate a user-click on it</span></div><div class="line">UiObject video = videos.getChildByText(<span class="keyword">new</span> UiSelector()</div><div class="line">        .className(<span class="string">"android.widget.LinearLayout"</span>), <span class="string">"Cute Baby Laughing"</span>);</div><div class="line">video.click();</div><div class="line"></div><div class="line"><span class="comment">// Simulate selecting a checkbox that is associated with the video</span></div><div class="line">UiObject checkBox = video.getChild(<span class="keyword">new</span> UiSelector()</div><div class="line">        .className(<span class="string">"android.widget.Checkbox"</span>));</div><div class="line"><span class="keyword">if</span>(!checkBox.isSelected()) checkbox.click();</div></pre></td></tr></table></figure></p>
<h2 id="操作可以滚动的UI元素"><a href="#操作可以滚动的UI元素" class="headerlink" title="操作可以滚动的UI元素"></a>操作可以滚动的UI元素</h2><p>可以使用<code>UiScrollable</code>来模拟用户对界面的滚动操作（水平、垂直）,这个技术可以帮我们搞定，当某个界面没有显示的时候，我们可以滚动界面，把那个界面显示出来。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">UiScrollable settingsItem = <span class="keyword">new</span> UiScrollable(<span class="keyword">new</span> UiSelector()</div><div class="line">        .className(<span class="string">"android.widget.ListView"</span>));</div><div class="line">UiObject about = settingsItem.getChildByText(<span class="keyword">new</span> UiSelector()</div><div class="line">        .className(<span class="string">"android.widget.LinearLayout"</span>), <span class="string">"About tablet"</span>);</div><div class="line">about.click();</div></pre></td></tr></table></figure>
<h1 id="校验结果"><a href="#校验结果" class="headerlink" title="校验结果"></a>校验结果</h1><p>Ui Automator 是基于InstrumentationTestCase，同样InstrumentationTestCase是基于标准的JUnit Assert，那么我们也可以使用标准的JUnit Assert来判断结果。</p>
<p>下面代码片段演示一个计算器的代码，以及验证结果。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> String CALC_PACKAGE = <span class="string">"com.myexample.calc"</span>;</div><div class="line"></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testTwoPlusThreeEqualsFive</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="comment">// Enter an equation: 2 + 3 = ?</span></div><div class="line">    mDevice.findObject(<span class="keyword">new</span> UiSelector()</div><div class="line">            .packageName(CALC_PACKAGE).resourceId(<span class="string">"two"</span>)).click();</div><div class="line">    mDevice.findObject(<span class="keyword">new</span> UiSelector()</div><div class="line">            .packageName(CALC_PACKAGE).resourceId(<span class="string">"plus"</span>)).click();</div><div class="line">    mDevice.findObject(<span class="keyword">new</span> UiSelector()</div><div class="line">            .packageName(CALC_PACKAGE).resourceId(<span class="string">"three"</span>)).click();</div><div class="line">    mDevice.findObject(<span class="keyword">new</span> UiSelector()</div><div class="line">            .packageName(CALC_PACKAGE).resourceId(<span class="string">"equals"</span>)).click();</div><div class="line"></div><div class="line">    <span class="comment">// Verify the result = 5</span></div><div class="line">    UiObject result = mDevice.findObject(By.res(CALC_PACKAGE, <span class="string">"result"</span>));</div><div class="line">    assertEquals(<span class="string">"5"</span>, result.getText());</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<h1 id="Api"><a href="#Api" class="headerlink" title="Api"></a>Api</h1><h2 id="UiDevice"><a href="#UiDevice" class="headerlink" title="UiDevice"></a>UiDevice</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div><div class="line">143</div><div class="line">144</div><div class="line">145</div><div class="line">146</div><div class="line">147</div><div class="line">148</div><div class="line">149</div><div class="line">150</div><div class="line">151</div><div class="line">152</div><div class="line">153</div><div class="line">154</div><div class="line">155</div><div class="line">156</div><div class="line">157</div><div class="line">158</div><div class="line">159</div><div class="line">160</div><div class="line">161</div><div class="line">162</div><div class="line">163</div><div class="line">164</div><div class="line">165</div><div class="line">166</div><div class="line">167</div><div class="line">168</div><div class="line">169</div><div class="line">170</div><div class="line">171</div><div class="line">172</div><div class="line">173</div><div class="line">174</div><div class="line">175</div><div class="line">176</div><div class="line">177</div><div class="line">178</div><div class="line">179</div><div class="line">180</div><div class="line">181</div><div class="line">182</div><div class="line">183</div><div class="line">184</div><div class="line">185</div><div class="line">186</div><div class="line">187</div><div class="line">188</div><div class="line">189</div><div class="line">190</div><div class="line">191</div><div class="line">192</div><div class="line">193</div><div class="line">194</div><div class="line">195</div><div class="line">196</div><div class="line">197</div><div class="line">198</div><div class="line">199</div><div class="line">200</div><div class="line">201</div><div class="line">202</div><div class="line">203</div><div class="line">204</div><div class="line">205</div><div class="line">206</div><div class="line">207</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">clearLastTraversedText</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Clears the text from the last UI traversal event.</span></span></div><div class="line"><span class="function"><span class="comment">// 清楚上次UI遍历的事件？</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">click</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span> y)</span></span></div><div class="line"><span class="function"><span class="comment">// Perform a click at arbitrary coordinates specified by the user</span></span></div><div class="line"><span class="function"><span class="comment">// 根据坐标点击</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">drag</span><span class="params">(<span class="keyword">int</span> startX, <span class="keyword">int</span> startY, <span class="keyword">int</span> endX, <span class="keyword">int</span> endY, <span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs a swipe from one coordinate to another coordinate.</span></span></div><div class="line"><span class="function"><span class="comment">// 拖动</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">dumpWindowHierarchy</span><span class="params">(File dest)</span></span></div><div class="line"><span class="function"><span class="comment">// Dump the current window hierarchy to a File.</span></span></div><div class="line"><span class="function"><span class="comment">// dump当前的层次化结构到文件中</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">dumpWindowHierarchy</span><span class="params">(OutputStream out)</span></span></div><div class="line"><span class="function"><span class="comment">// Dump the current window hierarchy to an OutputStream.</span></span></div><div class="line"><span class="function"><span class="comment">// dump当前的层次化结构到流中</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">dumpWindowHierarchy</span><span class="params">(String fileName)</span></span></div><div class="line"><span class="function"><span class="comment">// This method is deprecated. Use dumpWindowHierarchy(File) or dumpWindowHierarchy(OutputStream) instead.</span></span></div><div class="line"><span class="function"><span class="comment">// dump当前的层次化结构到文件中</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">UiObject2	<span class="title">findObject</span><span class="params">(BySelector selector)</span></span></div><div class="line"><span class="function"><span class="comment">// Returns the first object to match the selector criteria.</span></span></div><div class="line"><span class="function"><span class="comment">// 根据BySelector查找</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">UiObject	<span class="title">findObject</span><span class="params">(UiSelector selector)</span></span></div><div class="line"><span class="function"><span class="comment">// Returns a UiObject which represents a view that matches the specified selector criteria.</span></span></div><div class="line"><span class="function"><span class="comment">// 根据UiSelector 查找</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">List&lt;UiObject2&gt;	<span class="title">findObjects</span><span class="params">(BySelector selector)</span></span></div><div class="line"><span class="function"><span class="comment">// Returns all objects that match the selector criteria.</span></span></div><div class="line"><span class="function"><span class="comment">// 根据BySelector查找</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">freezeRotation</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Disables the sensors and freezes the device rotation at its current rotation state.</span></span></div><div class="line"><span class="function"><span class="comment">// 冻结旋转的状态</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">String	<span class="title">getCurrentActivityName</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// This method is deprecated. The results returned should be considered unreliable</span></span></div><div class="line"><span class="function"><span class="comment">// 获取当前Activity的名字，已经被废弃</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">String	<span class="title">getCurrentPackageName</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Retrieves the name of the last package to report accessibility events.</span></span></div><div class="line"><span class="function"><span class="comment">// 获取当前package</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">int</span>	<span class="title">getDisplayHeight</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Gets the height of the display, in pixels.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">int</span>	<span class="title">getDisplayRotation</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Returns the current rotation of the display, as defined in Surface</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">Point	<span class="title">getDisplaySizeDp</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Returns the display size in dp (device-independent pixel) The returned display size is adjusted per screen rotation.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">int</span>	<span class="title">getDisplayWidth</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Gets the width of the display, in pixels.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">static</span> UiDevice	<span class="title">getInstance</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// This method is deprecated. Should use getInstance(Instrumentation) instead. This version hides UiDevice's dependency on having an Instrumentation reference and is prone to misuse.</span></span></div><div class="line"><span class="function"><span class="comment">// 获取一个对象</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">static</span> UiDevice	<span class="title">getInstance</span><span class="params">(Instrumentation instrumentation)</span></span></div><div class="line"><span class="function"><span class="comment">// Retrieves a singleton instance of UiDevice</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">String	<span class="title">getLastTraversedText</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Retrieves the text from the last UI traversal event received.</span></span></div><div class="line"><span class="function"><span class="comment">// 获取上一次遍历的文本</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">String	<span class="title">getLauncherPackageName</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Retrieves default launcher package name</span></span></div><div class="line"><span class="function"><span class="comment">// 获取运行的packagename</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">String	<span class="title">getProductName</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Retrieves the product name of the device.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">hasAnyWatcherTriggered</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Checks if any registered UiWatcher have triggered.</span></span></div><div class="line"><span class="function"><span class="comment">// 检查是否有触发器触发</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">hasObject</span><span class="params">(BySelector selector)</span></span></div><div class="line"><span class="function"><span class="comment">// Returns whether there is a match for the given selector criteria.</span></span></div><div class="line"><span class="function"><span class="comment">// 是否有符合的条件的</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">hasWatcherTriggered</span><span class="params">(String watcherName)</span></span></div><div class="line"><span class="function"><span class="comment">// Checks if a specific registered UiWatcher has triggered.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isNaturalOrientation</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Check if the device is in its natural orientation.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isScreenOn</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Checks the power manager if the screen is ON.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">openNotification</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Opens the notification shade.</span></span></div><div class="line"><span class="function"><span class="comment">// 打开通知</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">openQuickSettings</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Opens the Quick Settings shade.</span></span></div><div class="line"><span class="function"><span class="comment">// 打开设置</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">&lt;R&gt; R	<span class="title">performActionAndWait</span><span class="params">(Runnable action, EventCondition&lt;R&gt; condition, <span class="keyword">long</span> timeout)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs the provided action and waits for the condition to be met.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressBack</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the BACK button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressDPadCenter</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the CENTER button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressDPadDown</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the DOWN button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressDPadLeft</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the LEFT button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressDPadRight</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the RIGHT button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressDPadUp</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the UP button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressDelete</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the DELETE key.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressEnter</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the ENTER key.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressHome</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the HOME button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressKeyCode</span><span class="params">(<span class="keyword">int</span> keyCode)</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press using a key code.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressKeyCode</span><span class="params">(<span class="keyword">int</span> keyCode, <span class="keyword">int</span> metaState)</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press using a key code.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressMenu</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the MENU button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressRecentApps</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the Recent Apps button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pressSearch</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates a short press on the SEARCH button.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">registerWatcher</span><span class="params">(String name, UiWatcher watcher)</span></span></div><div class="line"><span class="function"><span class="comment">// Registers a UiWatcher to run automatically when the testing framework is unable to find a match using a UiSelector.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">removeWatcher</span><span class="params">(String name)</span></span></div><div class="line"><span class="function"><span class="comment">// Removes a previously registered UiWatcher.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">resetWatcherTriggers</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Resets a UiWatcher that has been triggered.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">runWatchers</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// This method forces all registered watchers to run.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">setCompressedLayoutHeirarchy</span><span class="params">(<span class="keyword">boolean</span> compressed)</span></span></div><div class="line"><span class="function"><span class="comment">// Enables or disables layout hierarchy compression.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">setOrientationLeft</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates orienting the device to the left and also freezes rotation by disabling the sensors.</span></span></div><div class="line"><span class="function"><span class="comment">// 设置旋转方向</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">setOrientationNatural</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates orienting the device into its natural orientation and also freezes rotation by disabling the sensors.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">setOrientationRight</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Simulates orienting the device to the right and also freezes rotation by disabling the sensors.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">sleep</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// This method simply presses the power button if the screen is ON else it does nothing if the screen is already OFF.</span></span></div><div class="line"><span class="function"><span class="comment">// 关闭屏幕</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">swipe</span><span class="params">(<span class="keyword">int</span> startX, <span class="keyword">int</span> startY, <span class="keyword">int</span> endX, <span class="keyword">int</span> endY, <span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs a swipe from one coordinate to another using the number of steps to determine smoothness and speed.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">swipe</span><span class="params">(Point[] segments, <span class="keyword">int</span> segmentSteps)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs a swipe between points in the Point array.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">takeScreenshot</span><span class="params">(File storePath, <span class="keyword">float</span> scale, <span class="keyword">int</span> quality)</span></span></div><div class="line"><span class="function"><span class="comment">// Take a screenshot of current window and store it as PNG The screenshot is adjusted per screen rotation</span></span></div><div class="line"><span class="function"><span class="comment">// 截屏</span></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">takeScreenshot</span><span class="params">(File storePath)</span></span></div><div class="line"><span class="function"><span class="comment">// Take a screenshot of current window and store it as PNG Default scale of 1.0f (original size) and 90% quality is used The screenshot is adjusted per screen rotation</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">unfreezeRotation</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Re-enables the sensors and un-freezes the device rotation allowing its contents to rotate with the device physical rotation.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">&lt;R&gt; R	<span class="title">wait</span><span class="params">(SearchCondition&lt;R&gt; condition, <span class="keyword">long</span> timeout)</span></span></div><div class="line"><span class="function"><span class="comment">// Waits for given the condition to be met.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">waitForIdle</span><span class="params">(<span class="keyword">long</span> timeout)</span></span></div><div class="line"><span class="function"><span class="comment">// Waits for the current application to idle.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">waitForIdle</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Waits for the current application to idle.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">waitForWindowUpdate</span><span class="params">(String packageName, <span class="keyword">long</span> timeout)</span></span></div><div class="line"><span class="function"><span class="comment">// Waits for a window content update event to occur.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">wakeUp</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// This method simulates pressing the power button if the screen is OFF else it does nothing if the screen is already ON.</span></span></div><div class="line"><span class="function"><span class="comment">// 点亮屏幕</span></span></div></pre></td></tr></table></figure>
<h2 id="UiObject"><a href="#UiObject" class="headerlink" title="UiObject"></a>UiObject</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div><div class="line">99</div><div class="line">100</div><div class="line">101</div><div class="line">102</div><div class="line">103</div><div class="line">104</div><div class="line">105</div><div class="line">106</div><div class="line">107</div><div class="line">108</div><div class="line">109</div><div class="line">110</div><div class="line">111</div><div class="line">112</div><div class="line">113</div><div class="line">114</div><div class="line">115</div><div class="line">116</div><div class="line">117</div><div class="line">118</div><div class="line">119</div><div class="line">120</div><div class="line">121</div><div class="line">122</div><div class="line">123</div><div class="line">124</div><div class="line">125</div><div class="line">126</div><div class="line">127</div><div class="line">128</div><div class="line">129</div><div class="line">130</div><div class="line">131</div><div class="line">132</div><div class="line">133</div><div class="line">134</div><div class="line">135</div><div class="line">136</div><div class="line">137</div><div class="line">138</div><div class="line">139</div><div class="line">140</div><div class="line">141</div><div class="line">142</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">void</span>	<span class="title">clearTextField</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Clears the existing text contents in an editable field.</span></span></div><div class="line"><span class="function"><span class="comment">// 清空输入接口</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">click</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Performs a click at the center of the visible bounds of the UI element represented by this UiObject.</span></span></div><div class="line"><span class="function"><span class="comment">// 点击</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">clickAndWaitForNewWindow</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Waits for window transitions that would typically take longer than the usual default timeouts.</span></span></div><div class="line"><span class="function"><span class="comment">// 点击并等待新界面</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">clickAndWaitForNewWindow</span><span class="params">(<span class="keyword">long</span> timeout)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs a click at the center of the visible bounds of the UI element represented by this UiObject and waits for window transitions.</span></span></div><div class="line"><span class="function"><span class="comment">// 点击并等待新界面，设置等待时间</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">clickBottomRight</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Clicks the bottom and right corner of the UI element</span></span></div><div class="line"><span class="function"><span class="comment">// 点击右下边</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">clickTopLeft</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Clicks the top and left corner of the UI element</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">dragTo</span><span class="params">(UiObject destObj, <span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Drags this object to a destination UiObject.</span></span></div><div class="line"><span class="function"><span class="comment">// 拖动</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">dragTo</span><span class="params">(<span class="keyword">int</span> destX, <span class="keyword">int</span> destY, <span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Drags this object to arbitrary coordinates.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">exists</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Check if view exists.</span></span></div><div class="line"><span class="function"><span class="comment">// 判断是否存在</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">Rect	<span class="title">getBounds</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Returns the view's bounds property.</span></span></div><div class="line"><span class="function"><span class="comment">// 返回边界</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">UiObject	<span class="title">getChild</span><span class="params">(UiSelector selector)</span></span></div><div class="line"><span class="function"><span class="comment">// Creates a new UiObject for a child view that is under the present UiObject.</span></span></div><div class="line"><span class="function"><span class="comment">// 根据条件获取子元素</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">int</span>	<span class="title">getChildCount</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Counts the child views immediately under the present UiObject.</span></span></div><div class="line"><span class="function"><span class="comment">// 获取子元素数量</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">String	<span class="title">getClassName</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Retrieves the className property of the UI element.</span></span></div><div class="line"><span class="function"><span class="comment">// 获取当前元素的class name</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">String	<span class="title">getContentDescription</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Reads the content_desc property of the UI element</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">UiObject	<span class="title">getFromParent</span><span class="params">(UiSelector selector)</span></span></div><div class="line"><span class="function"><span class="comment">// Creates a new UiObject for a sibling view or a child of the sibling view, relative to the present UiObject.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">String	<span class="title">getPackageName</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Reads the view's package property</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">final</span> UiSelector	<span class="title">getSelector</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Debugging helper.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">String	<span class="title">getText</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Reads the text property of the UI element</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function">Rect	<span class="title">getVisibleBounds</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Returns the visible bounds of the view.</span></span></div><div class="line"><span class="function"><span class="comment">// 获取可见边界</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isCheckable</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Checks if the UI element's checkable property is currently true.</span></span></div><div class="line"><span class="function"><span class="comment">// 是否可以点击</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isChecked</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Check if the UI element's checked property is currently true</span></span></div><div class="line"><span class="function"><span class="comment">// 是否已经选中</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isClickable</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Checks if the UI element's clickable property is currently true.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isEnabled</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Checks if the UI element's enabled property is currently true.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isFocusable</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Check if the UI element's focusable property is currently true.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isFocused</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Check if the UI element's focused property is currently true</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isLongClickable</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Check if the view's long-clickable property is currently true</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isScrollable</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Check if the view's scrollable property is currently true</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">isSelected</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Checks if the UI element's selected property is currently true.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">longClick</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Long clicks the center of the visible bounds of the UI element</span></span></div><div class="line"><span class="function"><span class="comment">// 长按</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">longClickBottomRight</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Long clicks bottom and right corner of the UI element</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">longClickTopLeft</span><span class="params">()</span></span></div><div class="line"><span class="function"><span class="comment">// Long clicks on the top and left corner of the UI element</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">performMultiPointerGesture</span><span class="params">(PointerCoords... touches)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs a multi-touch gesture.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">performTwoPointerGesture</span><span class="params">(Point startPoint1, Point startPoint2, Point endPoint1, Point endPoint2, <span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Generates a two-pointer gesture with arbitrary starting and ending points.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pinchIn</span><span class="params">(<span class="keyword">int</span> percent, <span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs a two-pointer gesture, where each pointer moves diagonally toward the other, from the edges to the center of this UiObject .</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">pinchOut</span><span class="params">(<span class="keyword">int</span> percent, <span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs a two-pointer gesture, where each pointer moves diagonally opposite across the other, from the center out towards the edges of the this UiObject.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">setText</span><span class="params">(String text)</span></span></div><div class="line"><span class="function"><span class="comment">// Sets the text in an editable field, after clearing the field's content.</span></span></div><div class="line"><span class="function"><span class="comment">// 设置输入内容</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">swipeDown</span><span class="params">(<span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs the swipe down action on the UiObject.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">swipeLeft</span><span class="params">(<span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs the swipe left action on the UiObject.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">swipeRight</span><span class="params">(<span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs the swipe right action on the UiObject.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">swipeUp</span><span class="params">(<span class="keyword">int</span> steps)</span></span></div><div class="line"><span class="function"><span class="comment">// Performs the swipe up action on the UiObject.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">waitForExists</span><span class="params">(<span class="keyword">long</span> timeout)</span></span></div><div class="line"><span class="function"><span class="comment">// Waits a specified length of time for a view to become visible.</span></span></div><div class="line"><span class="function"></span></div><div class="line"><span class="function"><span class="keyword">boolean</span>	<span class="title">waitUntilGone</span><span class="params">(<span class="keyword">long</span> timeout)</span></span></div><div class="line"><span class="function"><span class="comment">// Waits a specified length of time for a view to become undetectable.</span></span></div></pre></td></tr></table></figure>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p><strong>优点：</strong></p>
<ul>
<li>可以对所有操作进行自动化，操作简单</li>
<li>不需要对被测程序进行重签名，且，可以测试所有设备上的程序，比如~某APP，比如~拨号，比如~发信息等等</li>
<li>对于控件定位，要比robotium简单一点点</li>
</ul>
<p><strong>缺点：</strong></p>
<ul>
<li>Ui Automator需要android level 16以上才可以使用，因为在level 16及以上的API里面才带有uiautomator工具</li>
<li>如果想要使用resource-id定位控件，则需要level 18及以上才可以</li>
<li>对中文支持不好（不代表不支持，第三方jar可以实现）</li>
</ul>
<h1 id="链接"><a href="#链接" class="headerlink" title="链接"></a>链接</h1><p><a href="https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html" target="_blank" rel="external">https://developer.android.com/training/testing/ui-testing/uiautomator-testing.html</a>``</p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat_pay.jpeg" alt="流水不腐小夏 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpeg" alt="流水不腐小夏 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Android测试/" rel="tag"># Android测试</a>
          
            <a href="/tags/自动化测试/" rel="tag"># 自动化测试</a>
          
            <a href="/tags/Android-UIAutomator/" rel="tag"># Android UIAutomator</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2016/07/25/AndroidMonkeyRunner/" rel="next" title="AndroidMonkeyRunner">
                <i class="fa fa-chevron-left"></i> AndroidMonkeyRunner
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2016/07/25/Android-Espresso/" rel="prev" title="Android Espresso">
                Android Espresso <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpeg"
                alt="流水不腐小夏" />
            
              <p class="site-author-name" itemprop="name">流水不腐小夏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiushuiXiaoxia" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/58771f980bac" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-简书"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/guijiaoba" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Android-UIAutomator浅谈"><span class="nav-number">1.</span> <span class="nav-text">Android UIAutomator浅谈</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#简介"><span class="nav-number">2.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#准备"><span class="nav-number">3.</span> <span class="nav-text">准备</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#UI元素查找"><span class="nav-number">4.</span> <span class="nav-text">UI元素查找</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#访问UI控件"><span class="nav-number">4.1.</span> <span class="nav-text">访问UI控件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#指定一个选择器"><span class="nav-number">4.2.</span> <span class="nav-text">指定一个选择器</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#执行动作"><span class="nav-number">5.</span> <span class="nav-text">执行动作</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#集合操作"><span class="nav-number">5.1.</span> <span class="nav-text">集合操作</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#操作可以滚动的UI元素"><span class="nav-number">5.2.</span> <span class="nav-text">操作可以滚动的UI元素</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#校验结果"><span class="nav-number">6.</span> <span class="nav-text">校验结果</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Api"><span class="nav-number">7.</span> <span class="nav-text">Api</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#UiDevice"><span class="nav-number">7.1.</span> <span class="nav-text">UiDevice</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#UiObject"><span class="nav-number">7.2.</span> <span class="nav-text">UiObject</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">8.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#链接"><span class="nav-number">9.</span> <span class="nav-text">链接</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">流水不腐小夏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://mycommons-cn.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://mycommons.cn/2016/07/25/Android-UIAutomator/';
          this.page.identifier = '2016/07/25/Android-UIAutomator/';
          this.page.title = 'Android UIAutomator';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://mycommons-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.3"></script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
